﻿using System;
using System.Collections.Generic;
using WickedFlame.Core.Layers;
using WickedFlame.Core.Shapes;
using WickedFlame.Core.State;

namespace WickedFlame.Util.Data
{
    [Serializable]
    public class SerializableShapeContainer
    {
        List<ShapeMemento> mementos;
        int _index = -1;
		
        public void Push(ShapeMemento data)
        {
            mementos.Add(data);
        }

        public void Push(VectorShape shape)
        {
			if (!(shape is IStateCapturable))
				throw new Exception(string.Format("Shape {0} does not implement th IStateCapturable interface!", shape.GetType().ToString()));

            if (mementos == null)
                mementos = new List<ShapeMemento>();

            mementos.Add(((IStateCapturable)shape).CreateMementoState());
        }

        /// <summary>
        /// Adds all the shapes contained in the layer to the serializeable object
        /// </summary>
        /// <param name="layer"></param>
        public void Push(Layer layer)
        {
            if (mementos == null)
                mementos = new List<ShapeMemento>();

            foreach (VectorShape shape in layer.ShapeList)
            {
				if (!(shape is IStateCapturable))
					throw new Exception(string.Format("Shape {0} does not implement th IStateCapturable interface!", shape.GetType().ToString()));

                mementos.Add(((IStateCapturable)shape).CreateMementoState());
            }
        }

        public VectorShape PullNext()
        {
            VectorShape shape = null;
            //if (_index < mementos.Count - 1)

            if (_index < 0)
                _index = 0;

            if (_index < mementos.Count)
            {
                shape = mementos[_index].GenerateShape();
                _index++;
            }

            return shape;
        }
    }
}
